

#include <gtk/gtk.h>
#include "page_state.h"

/*######################### Globals #############################*/
gint instruction_enable;
gint condition_enable;
gint hexpad_enable;
gint a_enable,b_enable,c_enable,d_enable,e_enable,h_enable,l_enable,m_enable;
gint sp_enable,psw_enable;
gint column;
gint row;
gint op_complete;
gint if_new_file;

gint save_state;
gint edit_state;
gint new_file=1;
gchar filename[50];
gchar *absolute_filename;


/*################## Set data on the widget ######################*/
void set_state_data(GtkWidget *label)
{
	if(label==NULL) /*if it is called first time*/
		return;
	
	struct page_state *state;
	state=g_malloc(sizeof(*state)); 
	state->save_state=save_state;
	state->edit_state=edit_state;
	state->instruction_enable=instruction_enable;
	state->condition_enable=condition_enable;
	state->hexpad_enable=hexpad_enable;
	state->a_enable=a_enable;
	state->b_enable=b_enable;
	state->c_enable=c_enable;
	state->d_enable=d_enable;
	state->e_enable=e_enable;
	state->h_enable=h_enable;
	state->l_enable=l_enable;
	state->m_enable=m_enable;
	state->sp_enable=sp_enable;
	state->psw_enable=psw_enable;
	state->column=column;
	state->row=row;
	state->op_complete=op_complete;
	state->if_new_file=if_new_file;
	g_strlcpy(state->filename,filename,49);
	state->absolute_filename=absolute_filename;

	g_object_set_data(G_OBJECT(label),"state_info",state);
	return;
}

/*################## Get data from the widget ######################*/
void get_state_data(GtkWidget *label)
{
	
	struct page_state *state;
	state=g_object_get_data(G_OBJECT(label),"state_info");
	if(state==NULL) /*called on newly opened file*/
	{
		GList *box_childs;
		box_childs=gtk_container_get_children(GTK_CONTAINER(label));
		g_strlcpy(filename,gtk_label_get_text(GTK_LABEL(box_childs->data)),49);
		save_state=0;
		edit_state=0;
		instruction_enable=0;
		condition_enable=1;
		hexpad_enable=1;
		a_enable=1;
		b_enable=1;
		c_enable=1;
		d_enable=1;
		e_enable=1;
		h_enable=1;
		l_enable=1;
		m_enable=1;
		sp_enable=1;
		psw_enable=1;
		column=2;
		row=-1;
		op_complete=1;
		if_new_file=new_file;
		absolute_filename=NULL;
		g_list_free(box_childs);
		return;
	}

	save_state=state->save_state;
	edit_state=state->edit_state;
	instruction_enable=state->instruction_enable;
	condition_enable=state->condition_enable;
	hexpad_enable=state->hexpad_enable;
	a_enable=state->a_enable;
	b_enable=state->b_enable;
	c_enable=state->c_enable;
	d_enable=state->d_enable;
	e_enable=state->e_enable;
	h_enable=state->h_enable;
	l_enable=state->l_enable;
	m_enable=state->m_enable;
	sp_enable=state->sp_enable;
	psw_enable=state->psw_enable;
	column=state->column;
	row=state->row;
	if_new_file=state->if_new_file;
	op_complete=state->op_complete;
	g_strlcpy(filename,state->filename,49);
	absolute_filename=state->absolute_filename;
	
	/*free state */
	g_free(state);
	return;
}
